package com.jielin.handling.service.leaveregion.impl;

import com.alibaba.fastjson.JSONObject;
import com.jielin.handling.entity.leaveregion.LeaveRegion;
import com.jielin.handling.entity.tsuspect.TSuspec;
import com.jielin.handling.mapper.leaveregion.LeaveRegionMapper;
import com.jielin.handling.mapper.tsuspect.TSuspecMapper;
import com.jielin.handling.service.leaveregion.LeaveregionService;
import com.jielin.handling.service.material.MaterialTypeService;
import com.jielin.handling.util.ConstantUtil;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wangjienice on 2019/3/20.
 */
@Service
@Slf4j
public class LeaveregionServiceImpl implements LeaveregionService {
	@Autowired
	LeaveRegionMapper leaveRegionMapper;
	@Autowired
	TSuspecMapper tSuspecMapper;
	@Autowired
	MaterialTypeService materialTypeService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int addLeaveregion(LeaveRegion leaveRegion) {
		log.info("leaveRegion:{}", JSONObject.toJSONString(leaveRegion));
		leaveRegion.setId(StringUtil.generatorUUID());
		TSuspec tSuspec = new TSuspec();
		tSuspec.setId(leaveRegion.getSuspectId());
		TSuspec suspectState = tSuspecMapper.selectStatue(leaveRegion.getSuspectId());
		if(!"8".equals(suspectState.getSuspectState())) {
			tSuspec.setSuspectState("8");
		}else{
			tSuspec.setSuspectState(suspectState.getSuspectState());
		}
//		try {
			leaveRegionMapper.deleteLeaveregion(leaveRegion.getSuspectId());// 嫌疑人办案区更新删除历史记录
			tSuspecMapper.updateState(tSuspec);// 修改嫌疑人状态
			TSuspec suspec = tSuspecMapper.selectById(leaveRegion.getSuspectId());// 根据嫌疑人id查询嫌疑人的信息，做卷宗操作
			if(StringUtil.isEmpty(suspec.getName())||StringUtil.isEmpty(suspec.getPersonnelType())||StringUtil.isEmpty(suspec.getPhone())||
					StringUtil.isEmpty(suspec.getIdValue())){
				return 2;
			}
			if (suspec.getCaseCode() != null) {
				new Thread(new ExecuteThread(suspec.getCaseCode(), suspec.getCaseName(), suspec.getPoliceId(),
						suspec.getId())).start();
///				materialTypeService.findMeaterialmodel(suspec.getCaseCode(), suspec.getCaseName(), suspec.getPoliceId(),
///						suspec.getId());// 新增卷宗操作
			}
			log.info("leaveRegion>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", JSONObject.toJSONString(leaveRegion));
			return leaveRegionMapper.insert(leaveRegion);
//		} catch (Exception e) {
//			log.info("service 事务回滚:{}", e);
//			// 抛出异常让其回滚
//			throw new RuntimeException("事务回滚");
//		}
	}

	@Override
	public LeaveRegion selelctLeaveregion(String suspectId) {
		if (StringUtil.isEmpty(suspectId)) {
			return null;
		} else {
			LeaveRegion lRegion = leaveRegionMapper.selectBySuspectId(suspectId);
			return lRegion;
		}
	}

	public class ExecuteThread implements Runnable {// 创建电子卷宗
		String caseCode;
		String caseName;
		String policeId;
		String suspectId;

		public ExecuteThread(String caseCode, String caseName, String policeId, String suspectId) {
			this.caseCode = caseCode;
			this.caseName = caseName;
			this.policeId = policeId;
			this.suspectId = suspectId;
		}

		@Override
		public void run() {
			// 新增卷宗操作
			if (ConstantUtil.XSKYRESTORAGE) {
				materialTypeService.findMeaterialmodel2(caseCode, caseName, policeId, suspectId, 0);
			}else {
				materialTypeService.findMeaterialmodel(caseCode, caseName, policeId, suspectId, 0);
			}
		}
	}

}
